package com.ywh.floating.lifecycle

import android.app.Activity
import android.app.Application
import android.os.Bundle
import com.ywh.floating.ext.flog
import java.lang.ref.WeakReference

/**
 * Create by yangwenhao on 2022/8/5
 */
class FloatingActivityLifecycle(private var iFloatingLife: IFloatingAttach? = null) : Application.ActivityLifecycleCallbacks {
    internal var weakReference: WeakReference<Activity>? = null
    override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) {

    }

    override fun onActivityStarted(activity: Activity) {
        flog(this::class.java, "${activity.javaClass.name}-onActivityStarted")
        if (weakReference == null) {
            weakReference = WeakReference(activity)
            iFloatingLife?.attach(activity)
            return
        }
        iFloatingLife?.detach()
        iFloatingLife?.attach(activity)
        weakReference = WeakReference(activity)
    }

    override fun onActivityResumed(activity: Activity) {

    }

    override fun onActivityPaused(activity: Activity) {
    }

    override fun onActivityStopped(activity: Activity) {
    }

    override fun onActivitySaveInstanceState(activity: Activity, outState: Bundle) {
    }

    override fun onActivityDestroyed(activity: Activity) {
        flog(this::class.java, "${activity.javaClass.name}-onActivityDestroyed")
    }
}